package com.csdig.cms.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.csdig.cms.dao.ModelItemDAO;
import com.csdig.cms.model.ModelItem;
import com.csdig.db.DBUtils;
import com.csdig.db.model.Pagination;
import com.csdig.db.model.Sql;

@Repository
public class ModelItemDAOImpl implements ModelItemDAO {

	@Override
	public void add(ModelItem vo) throws Exception {
		Sql sql = new Sql(
				"insert into model_item(model_item_id,model_path,field,item_label,priority,def_value,opt_value,data_type,is_single,is_required)");
		sql.append("values(:model_item_id,:model_path,:field,:item_label,:priority,:def_value,:opt_value,:data_type,:is_single,:is_required)");
		sql.set("model_item_id", vo.getModelItemId());
		sql.set("model_path", vo.getModelPath());
		sql.set("field", vo.getField());
		sql.set("item_label", vo.getItemLabel());
		sql.set("priority", vo.getPriority());
		sql.set("def_value", vo.getDefValue());
		sql.set("opt_value", vo.getOptValue());
		sql.set("data_type", vo.getDataType());
		sql.set("is_single", vo.getIsSingle());
		sql.set("is_required", vo.getIsRequired());
		DBUtils.exeUpdate(sql);
	}

	@Override
	public void update(ModelItem vo) throws Exception {
		String sql = "update model_item set model_path=?,field=?,item_label=?,priority=?,def_value=?,opt_value=?,data_type=?,is_single=?,is_required=?  where model_item_id=?";
		DBUtils.exeUpdate(sql,
				new Object[] { vo.getModelPath(), vo.getField(), vo.getItemLabel(), vo.getPriority(), vo.getDefValue(),
						vo.getOptValue(), vo.getDataType(), vo.getIsSingle(), vo.getIsRequired(), vo.getModelItemId() });
	}

	@Override
	public void delete(java.lang.Integer modelItemId) throws Exception {
		String sql = "delete from model_item where model_item_id=?";
		DBUtils.exeUpdate(sql, new Object[] { modelItemId });
	}

	@Override
	public ModelItem findById(java.lang.Integer modelItemId) throws Exception {
		String sql = "select * from model_item where model_item_id=?";
		return DBUtils.queryBean(sql, ModelItem.class, modelItemId);
	}

	@Override
	public Pagination<ModelItem> findByCondition(ModelItem vo, int pageNo, int pageSize) throws Exception {
		Sql sql = new Sql("select * from model_item where 1=1 ");
		// /add condtion here
		return DBUtils.queryPagination(sql, null, pageNo, pageSize, ModelItem.class);
	}

	@Override
	public List<ModelItem> findByCondition(ModelItem vo) throws Exception {
		Sql sql = new Sql("select * from model_item where 1=1 ");
		// /add condtion here
		return DBUtils.queryBeanList(sql, ModelItem.class);
	}

	@Override
	public List<ModelItem> listAll() throws Exception {
		return findByCondition(null);
	}

	@Override
	public List<ModelItem> findbyPath(String path) throws Exception {
		Sql sql = new Sql("select * from model_item where model_path=:model_path");
		sql.set("model_path", path);
		return DBUtils.queryBeanList(sql, ModelItem.class);
	}

	@Override
	public ModelItem findUniqByCondi(String path, String field) throws Exception {
		Sql sql = new Sql("select * from model_item where model_path=:model_path and field=:field");
		sql.set("model_path", path);
		sql.set("field", field);
		return DBUtils.queryBean(sql, ModelItem.class);
	}

	@Override
	public List<ModelItem> findByCondition(String[] fieldNames, Object[] values) throws Exception {
		Sql sql = new Sql("select * from model_item where 1=1");
		for (int i = 0; i < fieldNames.length; i++) {
			String field = fieldNames[i];
			sql.append(" and " + field + "=:" + field);
			sql.set(field, values[i]);
		}
		return DBUtils.queryBeanList(sql,ModelItem.class);
	}

	@Override
	public List<ModelItem> findCanAddItem(String modelPath, Integer channelId) throws Exception {
		Sql sql = new Sql("select * from model_item a where a.is_single=:is_single and model_path=:model_path");
		sql.append(" and not exists(select 1 from channel_attr b where a.`field`=b.attr_name and b.channel_id=:channel_id)");
		sql.set("is_single", Boolean.TRUE);
		sql.set("model_path", modelPath);
		sql.set("channel_id", channelId);
		return DBUtils.queryBeanList(sql, ModelItem.class);
	}

}
